/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.kill;

import cn.ac.iie.di.global.GlobalParas;
import java.util.concurrent.TimeUnit;
import org.apache.log4j.Logger;
import sun.misc.Signal;
import sun.misc.SignalHandler;

/**
 *
 *
 */
public class KillHandler implements SignalHandler {

        public static Logger log = Logger.getLogger(KillHandler.class.getName());
        public boolean stopPull = false;

        public KillHandler() {
        }

        public void registerSignal(String signalName) {
                Signal signal = new Signal(signalName);
                Signal.handle(signal, this);
        }

        @Override
        public void handle(Signal signal) {
                log.info("now receive the system signal " + signal.getName() + " " + signal.getNumber() + "\n\n");
                if (signal.getName().equals("TERM")) {
                        log.info("shutting the rockert consumer ... ");
                        try {
                                //首先执行关闭拉取消息队列
                                while (!GlobalParas.pullMsgQueue.isEmpty()) {
                                        GlobalParas.pullMsgQueue.poll(20, TimeUnit.MILLISECONDS).shutDownConsumer();
                                        stopPull = true;
                                }
                                Thread.sleep(3000);
                                GlobalParas.killMonitor = true;
                                log.info("shut the rockert consumer ok... ");

                                while (true) {
                                        if (GlobalParas.oriMsgPool.isEmpty() || GlobalParas.pullMsgQueue.isEmpty()) {
                                                GlobalParas.killRemsg2Msg = true;
                                                break;
                                        }
                                }
                                while (true) {
                                        if (GlobalParas.dwdMsgPool.isEmpty() && GlobalParas.ReMsg2MsgQueue.isEmpty()) {
                                                GlobalParas.killDownPic = true;
                                                break;
                                        }
                                }
                                while (true) {
                                        if (GlobalParas.urlAndAddrPool.isEmpty() && GlobalParas.DwdPicQueue.isEmpty()) {
                                                GlobalParas.killDPAction = true;
                                                break;
                                        }
                                }
                                while (true) {
                                        if (GlobalParas.sendMsgPool.isEmpty() && GlobalParas.DwdPicAQueue.isEmpty()) {
                                                GlobalParas.killsendMsg = true;
                                                break;
                                        }
                                }
                                while (true) {
                                        if (GlobalParas.MqProducerQueue.isEmpty()) {

                                                GlobalParas.hasBeenExited = true;
                                                break;
                                        }
                                }
                        } catch (InterruptedException ex) {
                                log.error(ex, ex);
                        }
                } else if (signal.getName().equals("INT") || signal.getName().equals("HUP")) {
                        log.info("can not process the system signal " + signal.toString());
                } else {
                        log.info("can not process the system signal " + signal.toString());
                }
        }

        public static void main(String[] args) {
        }
}
